//
//  BKResultOfKongZhiXianViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/6.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKResultOfKongZhiXianViewController.h"
#import "BKKongZhiXianTableViewCell.h"
#import "BKBlackTextField.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKKongZhiXianViewModel.h"
#import <MBProgressHUD.h>

static NSString *kongzhixian = @"kongzhixian";
@interface BKResultOfKongZhiXianViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet BKBlackTextField *kq;
@property (weak, nonatomic) IBOutlet BKBlackTextField *wl;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,assign) BOOL isSearch;
@property (nonatomic ,strong) MBProgressHUD *hud;

@end

@implementation BKResultOfKongZhiXianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_hud];
    _hud.labelText = @"正在加载";
    _hud.mode = MBProgressHUDModeIndeterminate;
    
    
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKKongZhiXianTableViewCell class]) bundle:nil] forCellReuseIdentifier:kongzhixian];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _tableView.layer.cornerRadius = 3;
//    _tableView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
//    _tableView.layer.borderWidth = 1.5;
    
    [_tableView reloadData];
    
    RAC(self,isSearch) = [RACSignal combineLatest:@[_kq.rac_textSignal,_wl.rac_textSignal]
                                           reduce:^id(NSString *kq,NSString *wl){
                                               
                                               return @([kq length]>0 && [wl length]>0);
                                               
                                           }];
    
    @weakify(self);
    [RACObserve(self, isSearch) subscribeNext:^(id x) {
        if ([x intValue]!=0) {
            @strongify(self);
            [self.hud show:YES];
            NSString *wl = [self.wl.text isEqualToString:@"文科"]?@"1":@"2";
            [BKKongZhiXianViewModel kongzhixianWithdq:_kq.text wl:wl year:_year success:^(NSArray *dataArray) {
                self.dataArray = dataArray;
                dispatch_async(dispatch_get_main_queue(), ^{
                
                    [self.hud hide:YES];
                    self.shengqu = self.kq.text;
                    
                    [self.tableView reloadData];
                    
                    
                });

            
            } failure:^(NSString *message) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    
                    [self.hud hide:YES];
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [alertView show];
                });

            }];
        }
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [_dataArray count]+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    BKKongZhiXianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kongzhixian];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.row==0) {
        cell.wl.text = @"文理科";
        cell.pc.text = @"批次名称";
        cell.fen.text = @"最低控制分数线";
        return cell;
        
    }
    cell.wl.text = _dataArray[indexPath.row-1][@"type"];
    cell.pc.text = _dataArray[indexPath.row-1][@"batch"];
    cell.fen.text = _dataArray[indexPath.row-1][@"score"];
    
    if (indexPath.row%2==0) {
        cell.backgroundColor = [UIColor clearColor];
    }else{
    
        cell.backgroundColor = [UIColor colorWithRed:0.886 green:0.945 blue:0.961 alpha:.6];

    }
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 44;
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
    label.backgroundColor = [UIColor colorWithRed:0.910 green:0.965 blue:0.980 alpha:1.000];
    label.text = [NSString stringWithFormat:@"%@地区%@年各批次控制分数",_shengqu,_year];
    label.layer.borderWidth = .5;
    label.layer.borderColor = [[UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.5] CGColor];
    //.714, .922, .965, 1
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 44;
}
@end
